package net.kuratkoo.jidelnicekvut;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;
import java.util.List;

public class FoodListAdapter extends BaseAdapter {
    private static final String TAG = "JidelniceVUT|FoodListAdapter";
    private Context context;
    private List<Food> foodSet;
    private LayoutInflater mInflater;

    public FoodListAdapter(Context context, List<Food> foodSet) {
	this.context = context;
	this.foodSet = foodSet;
	mInflater = LayoutInflater.from(context);
    }

    public int getCount() {
	return this.foodSet.size();
    }

    public Food getItem(int i) {
	return this.foodSet.get(i);
    }

    public long getItemId(int i) {
	return i;
    }

    public View getView(int i, View convertView, ViewGroup parent) {
	FoodViewHolder holder;
	if (convertView == null) {
	    convertView = mInflater.inflate(R.layout.list_item_food, parent, false);

	    holder = new FoodViewHolder();
	    holder.food = (TextView) convertView.findViewById(R.id.food);
	    holder.price = (TextView) convertView.findViewById(R.id.price);
            holder.type = (TextView) convertView.findViewById(R.id.type);
            holder.voteCount = (TextView) convertView.findViewById(R.id.vote_count);
            holder.voteCountX = (TextView) convertView.findViewById(R.id.vote_count_x);
            holder.item = (LinearLayout) convertView.findViewById(R.id.item);
            holder.bar = (RatingBar) convertView.findViewById(R.id.smallBar);
	    convertView.setTag(holder);
	} else {
	    holder = (FoodViewHolder) convertView.getTag();
	}

	holder.food.setText(this.getItem(i).food);
        holder.type.setText(this.getItem(i).type);
        holder.voteCount.setText(this.getItem(i).voteCount.toString());

        /** RatingBar **/
        if (this.getItem(i).voteCount > 0) {
            holder.bar.setRating(this.getItem(i).votes);
            holder.bar.setVisibility(View.VISIBLE);
            holder.voteCount.setVisibility(View.VISIBLE);
            holder.voteCountX.setVisibility(View.VISIBLE);
        } else {
            holder.bar.setVisibility(View.GONE);
            holder.voteCount.setVisibility(View.GONE);
            holder.voteCountX.setVisibility(View.GONE);
        }
        /** Price **/
        if (Preferences.getString(context, "price", "stud").equals("stud")) {
            holder.price.setText(this.getItem(i).price1.toString());
        } else if (Preferences.getString(context, "price", "stud").equals("empl")) {
            holder.price.setText(this.getItem(i).price2.toString());
        } else if (Preferences.getString(context, "price", "stud").equals("other")) {
            holder.price.setText(this.getItem(i).price3.toString());
        } else {
            holder.price.setText(this.getItem(i).price1.toString());
        }

        /** Keywords **/
        if (this.getItem(i).isKeyword) {
            holder.item.setBackgroundResource(R.color.just_light_yellow);
        } else {
            holder.item.setBackgroundResource(R.color.just_white);
        }

	return convertView;
    }
}

class FoodViewHolder {
    TextView food;
    TextView type;
    TextView price;
    LinearLayout item;
    RatingBar bar;
    TextView voteCount;
    TextView voteCountX;
}
